package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.MessageNum;
import com.sywb.chuangyebao.contract.UserMessageContract;

import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 消息中心
 */
public class UserMessageActivity extends ActionbarActivity<UserMessageContract.Presenter> implements UserMessageContract.MessageView {

    @BindView(R.id.ll_notification)
    LinearLayout llNotification;
    @BindView(R.id.tv_notification)
    TextView tvNotification;
    @BindView(R.id.tv_notification_num)
    TextView tvNotificationNum;

    @BindView(R.id.ll_letter)
    LinearLayout llLetter;
    @BindView(R.id.tv_letter)
    TextView tvLetter;
    @BindView(R.id.tv_letter_num)
    TextView tvLetterNum;

    @BindView(R.id.ll_notice)
    LinearLayout llNotice;
    @BindView(R.id.tv_notice)
    TextView tvNotice;
    @BindView(R.id.tv_notice_num)
    TextView tvNoticeNum;

    @BindView(R.id.tab_cursor)
    View tabCursor;

    @BindView(R.id.view_pager)
    ViewPager viewPager;

    public String messageNumJson;
    public int currentItem;
    public String noticeJson;

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_user_message;
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        setTitle(getString(R.string.message_center));

        if (bundle == null) {
            bundle = getIntent().getExtras();
        }
        messageNumJson = bundle.getString("p0");
        currentItem = bundle.getInt("p1", 0);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mPresenter.changeView(position, tvNotification, tvLetter, tvNotice);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        setMesssageUnread(messageNumJson);

        if (currentItem == 2) {
            noticeJson = bundle.getString("p2");
        }
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
        if (currentItem == 2 && !TextUtils.isEmpty(noticeJson)) {
            mPresenter.changeView(2, tvNotification, tvLetter, tvNotice);
            advance(UserNoticeDetailActivity.class, noticeJson);
            mPresenter.refreshNotices();
        }
    }

    @OnClick({R.id.ll_notification, R.id.ll_letter, R.id.ll_notice,})
    public void onClick(View view) {
        if (!isCanClick(view)) return;
        switch (view.getId()) {
            case R.id.ll_notification:
                mPresenter.changeView(0, tvNotification, tvLetter, tvNotice);
                break;
            case R.id.ll_letter:
                mPresenter.changeView(1, tvNotification, tvLetter, tvNotice);
                break;
            case R.id.ll_notice:
                mPresenter.changeView(2, tvNotification, tvLetter, tvNotice);
                break;
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_CHAT_MESSAGE)})
    public void rxChatMessage(String data) {
        mPresenter.getChatConect();
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.TABLE_CHATSESSION)})
    public void rxChatSession(String data) {
        mPresenter.initChatData();
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_MY_MSGNUM)})
    public void rxMsgNumChnage(String data) {
        setMesssageUnread(data);
    }

    public void setMesssageUnread(String messageNumJson) {
        if (!TextUtils.isEmpty(messageNumJson)) {
            MessageNum messageNum = JSON.parseObject(messageNumJson, MessageNum.class);
            setMesssageUnread(messageNum.notify_num, messageNum.msg_num, messageNum.note_num);
        } else {
            setMesssageUnread(0, 0, 0);
            RxBus.get().post(Constants.USERCHAT, Constants.USERCHAT);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("p0", messageNumJson);
        outState.putInt("p1", currentItem);
        outState.putString("p2", noticeJson);
        super.onSaveInstanceState(outState);
    }

    @Override
    public ViewPager getViewPager() {
        return viewPager;
    }

    @Override
    public View getTabCursor() {
        return tabCursor;
    }

    @Override
    public void setMesssageUnread(int notificationCount, int letterCount, int noticeCount) {
        if (notificationCount == 0) {
            tvNotificationNum.setVisibility(View.GONE);
        } else {
            if (notificationCount > 99) {
                notificationCount = 99;
            }
            tvNotificationNum.setVisibility(View.VISIBLE);
            tvNotificationNum.setText(String.valueOf(notificationCount));
        }

        if (letterCount == 0) {
            tvLetterNum.setVisibility(View.GONE);
        } else {
            if (letterCount > 99) {
                letterCount = 99;
            }
            tvLetterNum.setVisibility(View.VISIBLE);
            tvLetterNum.setText(String.valueOf(letterCount));
        }

        if (noticeCount == 0) {
            tvNoticeNum.setVisibility(View.GONE);
        } else {
            if (noticeCount > 99) {
                noticeCount = 99;
            }
            tvNoticeNum.setVisibility(View.VISIBLE);
            tvNoticeNum.setText(String.valueOf(noticeCount));
        }
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

}
